define(
    function (require) {

        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var globalData = require('common/GlobalData').getInstance();

        var showid = 1;
        var page = 1;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 40,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '用户名',
                field: 'username' ,
                resizable : true,
                width: 150,
                content: function (item) {
                    return item.username;
                }
            },
            {
                title: '角色',
                field: 'role' ,
                sortable: true,
                resizable : true,
                width: 120,
                content: function (item) {
                    return psrole(item.role);
                }
            },
            {
                title: '负责人电话',
                field: 'phone' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.phone;
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="delete">删除</a>';
                }
            }
        ];
        //判断角色
        function  psrole(obj){
            if (obj === 1) {
                return "管理员";
            } else if (obj === 2) {
                return "经销商";
            } else {
                return "--";
            }
        }
        function search() {
            var mallName = this.get('mallName');
            if (mallName.getValue().length > 0) {
                this.fire('search', { mallName: mallName.getValue() });
            } else {
                alert("用户名不能为空！");
            }
        }
        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page});
        }

        var BaseListView = require('common/ListView');

        function MallListView() {
            BaseListView.apply(this, arguments);
        }

        MallListView.prototype.template = 'mallListPage';

        MallListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无该用户!</p>'
                }
            };
        }

        MallListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );
            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );
            //检索
            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );
            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );

            this.get('list').initChildren();
        }

        require('er/util').inherits(MallListView, BaseListView);
        return MallListView;
    }
);
